package proxy

import (
	"gitee.com/xuender/mysql-proxy/pb"
	"github.com/xelabs/go-mysqlstack/driver"
	"github.com/xelabs/go-mysqlstack/sqlparser/depends/query"
	"github.com/xelabs/go-mysqlstack/sqlparser/depends/sqltypes"
	"github.com/xuender/oils/logs"
)

type GRPCHandler struct {
	// client pb.CallerClient
	BaseHandler
}

func NewGRPCHandler(
// client pb.CallerClient
) *GRPCHandler {
	return &GRPCHandler{
		// client: client
	}
}

func (p *GRPCHandler) ComQuery(
	session *driver.Session,
	query string,
	bindVariables map[string]*query.BindVariable,
	callback func(*sqltypes.Result) error,
) error {
	logs.Debugw("ComQuery", "id", session.ID(), "query", query)

	// ctx, cancel := context.WithTimeout(context.Background(), time.Second*base.Four)
	// defer cancel()

	// msg := base.Panic1(p.client.Call(ctx, &pb.Msg{Sql: query}))
	msg := &pb.Msg{}
	logs.Debugw("call", "sql", msg.Sql)

	return callback(msg.ToResult())
}
